#!/bin/bash

FULL_PATH_TO_SCRIPT="$(realpath "${BASH_SOURCE[-1]}")"
SCRIPT_DIRECTORY="$(dirname "$FULL_PATH_TO_SCRIPT")"
SCRIPT_FILENAME="$(basename "$FULL_PATH_TO_SCRIPT")"
hostfile="$SCRIPT_DIRECTORY/hostfile"

hostlist=$(cat $hostfile | awk '{print $1}' | xargs)
cur_addr=$(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2;}'|tr -d "addr:")

if [[ $# -gt 1 ]]; then
    args=$@
    files=$(echo $args | awk 'NF{NF--};1')
    dest=$(realpath $(echo $args | awk '{print $NF}'))
    for host in ${hostlist[@]}; do
        if [[ $host == $cur_addr ]]; then
            continue
        fi
        cmd="scp -r ${files} ${host}:${dest}"
        printf -- '-%.0s' {1..100}; echo ""
        echo $cmd
        printf -- '-%.0s' {1..100}; echo ""
        eval $cmd
    done
fi